
#include "../inc/Ui/FindDirectionForm.h"
#include "../inc/Ui/WebForm.h"
#include "../inc/Utility/MyUtil.h"

int FindDirectionForm::FormId = -1;

FindDirectionForm::FindDirectionForm(void) {

}

FindDirectionForm::~FindDirectionForm(void) {

}

bool FindDirectionForm::Initialize() {
	Form::Construct(L"FindDirectionForm");

	return true;
}

result FindDirectionForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	docKetQua = false;
	strTransport = "m";


	pEditFieldViTriBatDau = static_cast<EditField*> (GetControl(L"ViTriBatDauEditField"));
	pEditFieldViTriDen = static_cast<EditField*>(GetControl(L"ViTriDenEditField"));

	pChkDiBo = static_cast<CheckButton*>(GetControl(L"DiBoCheckBox"));
	if (pChkDiBo != null)
	{
		pChkDiBo->SetActionId(ID_CHECKBUTTON_DIBO, ID_CHECKBUTTON_DIBO);
		pChkDiBo->AddActionEventListener(*this);
	}
	pChkXeMay = static_cast<CheckButton*>(GetControl(L"XeMayCheckBox"));
	if (pChkXeMay != null)
	{
		pChkXeMay->SetActionId(ID_CHECKBUTTON_XEMAY, ID_CHECKBUTTON_XEMAY, ID_CHECKBUTTON_XEMAY);
		pChkXeMay->SetSelected(true);
		pChkXeMay->AddActionEventListener(*this);

	}

	pChkDocKetQua = static_cast<CheckButton*>(GetControl(L"DocKetQuaCheckBox"));
	if (pChkDocKetQua != null)
	{
		pChkDocKetQua->SetActionId(ID_CHECKBUTTON_DOCKETQUA, ID_CHECKBUTTON_DOCKETQUA, ID_CHECKBUTTON_DOCKETQUA);
		pChkDocKetQua->AddActionEventListener(*this);
	}

	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_CANCEL);

	return r;
}

result FindDirectionForm::OnTerminating(void) {
	result r = E_SUCCESS;

	return r;
}

void FindDirectionForm::TimDuongDi() {
	String strErr = "";

	Application *pApp = Application::GetInstance();
	if (pEditFieldViTriBatDau->GetTextLength() == 0) {
		pApp->GetAppResource()->GetString(L"IDS_FINDDIRECTION_ERROR_STARTPOINT", strErr);
		strErr.Append('\n');
	}
	if (pEditFieldViTriDen->GetTextLength() == 0)
	{
		String temp = L"";
		pApp->GetAppResource()->GetString(L"IDS_FINDDIRECTION_ERROR_ENDPOINT", temp);
		strErr.Append(temp);
	}
	if (strErr.GetLength() > 0)
	{
		MessageBox msg;
		msg.Construct(L"Find Direction", strErr, MSGBOX_STYLE_OK);
		int i = 0;
		msg.ShowAndWait(i);
	}
	else
	{
		String strViTriBatDau = pEditFieldViTriBatDau->GetText();
		String strViTriDen = pEditFieldViTriDen->GetText();
		//String strTransport = "m";
		FormManager *pFormManager = null;
		FormManager::GetInstance(pFormManager);
		Form *pWebForm = null;
		result r = pFormManager->GetForm(WebForm::GetFormId(), pWebForm);
		if (IsFailed(r)){
			return ;
		}
		strViTriBatDau.Replace(L" ", L"+");
		strViTriDen.Replace(L" ", L"+");

		String viTriBatDau = L"";
		String viTriDen = L"";
		r = MyUtil::StringToUTF8String(strViTriBatDau, viTriBatDau);
		if (IsFailed(r)){
			viTriBatDau = strViTriBatDau;
		}

		r = MyUtil::StringToUTF8String(strViTriDen, viTriDen);
		if (IsFailed(r)){
			viTriDen = strViTriDen;
		}

		IList *list = new ArrayList();
		list->Add(*(new String(strViTriBatDau)));
		list->Add(*(new String(strViTriDen)));
		list->Add(*(new String(strTransport)));

		if (docKetQua){
			list->Add(*(new Boolean(true)));
		}else{
			list->Add(*(new Boolean(false)));
		}

		pWebForm->SendUserEvent(WebForm::REQUEST_FINDIRECTION, list);
/*
		if (docKetQua)
		{
			AppLog("Doc ket qua---------------------------------------------");
			bool isVietnamese = true;

			isVietnamese = MyUtil::GetSystemLanguage().Equals(L"vn", false);
			String language = L"vi";
			if (!isVietnamese){
				language = L"en";
			}
			String url = L"http://maps.google.com/maps?hl=";
			url.Append(language);
			url.Append(L"&saddr=");
			url.Append(viTriBatDau);
			url.Append(L"&daddr=");
			url.Append(viTriDen);
			url.Append(L"&ie=UTF8&dirflg=");
			url.Append(strTransport);
			url.Append(L"&output=kml");
			Downloader::downloadAndSave(url,L"/Home/test.kml", this);
		}
		AppLog("Khong vao doc ket qua------------------------------");

		AppLog("%d", WebForm::GetFormId());*/
		pFormManager->SetCurrentForm(WebForm::GetFormId());


		//(static_cast<WebForm*>(pParent))->VeDuongDi(strViTriBatDau, strViTriDen, strTransport);

		//(static_cast<WebForm*>(pWebForm))->VeDuongDi(strViTriBatDau, strViTriDen, strTransport);

	}
	//return E_SUCCESS;
}

/**
 * Override I
 */

void FindDirectionForm::OnActionPerformed(const Osp::Ui::Control &source, int actionId) {

	FormManager *pFormManager = null;
		switch (actionId) {
	case ID_SOFTKEY_OK:
		AppLog("Softkey ok");

		TimDuongDi();


		break;

	case ID_SOFTKEY_CANCEL:
		AppLog("Softkey cancel");

		FormManager::GetInstance(pFormManager);
		pFormManager->SetCurrentForm(WebForm::GetFormId());

		break;
	case ID_CHECKBUTTON_DIBO:
		strTransport = "w";
		break;

	case ID_CHECKBUTTON_XEMAY:
		strTransport = "m";
		break;

	case ID_CHECKBUTTON_DOCKETQUA:
		docKetQua = !docKetQua;

		break;

	default:
		break;
	}
}

void FindDirectionForm::OnItemStateChanged(const Osp::Ui::Control &source,
		int index, int itemId, Osp::Ui::ItemStatus status) {
	switch (itemId) {

	}
}
/*
void FindDirectionForm::onDownloadComplete(Osp::Base::String url,
		Osp::Base::String path) {
	MyParserXml parser;
	char *p = MyUtil::ConvertStringToCharN(path);
	LinkedListT<DiaChi> *lstDiaDiem = parser.layGiaTriVaoMangPlaceMarkN(p);
	DiaChi dc;
	for (int i = 0; i < lstDiaDiem->GetCount(); i++) {
		lstDiaDiem->GetAt(i, dc);
		//AppLog("Ko biet:%f, %f", dc.toaDo.lat, dc.toaDo.lng);
		//(static_cast<WebForm*> (pParent))->themToaDo(dc.toaDo.lat, dc.toaDo.lng);
	}
	//String from = pEditFieldViTriBatDau->GetText();
	//String to = pEditFieldViTriDen->GetText();
	pHuongDan = HuongDanDuongDiBangGiongNoi::GetInstance();
	pHuongDan->SetMode(HuongDanDuongDiBangGiongNoi::GOOGLE_MODE);
	GoogleReader* reader = static_cast<GoogleReader*>(pHuongDan->GetReader());
	reader->SetLanguage(GoogleReader::VIETNAMESE);
	pHuongDan->read(lstDiaDiem);
	pHuongDan->Run();
	delete lstDiaDiem;
	delete p;
}
void FindDirectionForm::onDownloadFailed(Osp::Base::String url,
		Osp::Base::String path) {
	AppLog("da luu that bai");
}*/
